package com.prestamomo.vil.ppa.pmvilmedium.pmvilarm;

import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;

import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmlocation.PmvilLocInfoMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmnet.PmvilNetInfoMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmother.PmvilGeneralMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmother.PmvilOtherDataMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmsave.PmvilStorageMd;

import org.json.JSONException;
import org.json.JSONObject;

public class PmvilDeviceMd {

    public static JSONObject getDevicePmvilObj() throws JSONException {
        JSONObject infoDevPmvilObj = new JSONObject();
        infoDevPmvilObj.put("nblodfoqm", PmvilCellReverMd.getBatterJson());//--battery_status
        infoDevPmvilObj.put("rjizdbfs", PmvilGeneralMd.getGeneraPmvilInfo(PmvilApp.getPmvilApss()));//--general_data
        JSONObject hardwarePmvilObj = getPmvilHardJsObect();
        infoDevPmvilObj.put("jcjev", hardwarePmvilObj);//--hardware
        infoDevPmvilObj.put("oqldtdqe", PmvilLocInfoMd.getInfoPmvilLoaction());//GPS--location
        infoDevPmvilObj.put("qgrgwovf", PmvilNetInfoMd.getInfoPmvilNet());//--network
        infoDevPmvilObj.put("jyezeovj", PmvilOtherDataMd.getInfoPmvilOther());//--other_data
        infoDevPmvilObj.put("cutwav", PmvilStorageMd.getInfoPmvilStorage());//--storage
        infoDevPmvilObj.put("qonxw", 0);//--video_external
        infoDevPmvilObj.put("iasdpia", 0);//--video_internal
        infoDevPmvilObj.put("vcmcq", 0);//--audio_external
        infoDevPmvilObj.put("sxzlxumbf", 0);//--audio_internal
        infoDevPmvilObj.put("kyhlhw", 0);//--images_external
        infoDevPmvilObj.put("jqqlpkdnv", 0);//--images_internal
        infoDevPmvilObj.put("dpujhnz", 0);//download_files
        infoDevPmvilObj.put("ooqidqdze", 0);//⼈--contact_group
        return infoDevPmvilObj;
    }

    @NonNull
    private static JSONObject getPmvilHardJsObect() throws JSONException {
        JSONObject hardwarePmvilObj = new JSONObject();
        String pmvilDeviceName = "";
        try {
            pmvilDeviceName = Settings.Global.getString(PmvilApp.getPmvilApss().getContentResolver(), Settings.Global.DEVICE_NAME);
        }catch (Exception e){
            try {
                pmvilDeviceName = Settings.Secure.getString(PmvilApp.getPmvilApss().getContentResolver(), "bluetooth_name");
            } catch (Exception e1) {
                pmvilDeviceName = Build.USER;
            }
        }
        hardwarePmvilObj.put("jjkcbuk", pmvilDeviceName);//--device_name
        hardwarePmvilObj.put("kwzxlrbx", Build.VERSION.SDK_INT);//--sdk_version
        hardwarePmvilObj.put("uqalcj", Build.MODEL);//--model
        hardwarePmvilObj.put("ibwdv", Build.VERSION.RELEASE);//--release
        hardwarePmvilObj.put("wowusu", Build.BRAND);//--brand
        String pmvilSerial = "";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            String pmvilSeral = Build.SERIAL;
            pmvilSerial = pmvilSerialIsEmst(pmvilSeral);
        } else {
            try {
                pmvilSerial = pmvilSerialIsEmst(Build.getSerial());
            } catch (Exception e) {
                pmvilSerial = pmvilSerialIsEmst("");
            }
        }
//        hardwarePmvilObj.put("kkevan", getDevicePmvilSerial());//--serial_number
        hardwarePmvilObj.put("cusrrn", pmvilSerial);//--serial_number
        hardwarePmvilObj.put("doaclyyzk", Build.PRODUCT);//--product
        hardwarePmvilObj.put("ofsikyh", Runtime.getRuntime().availableProcessors());//--cpu_type
        hardwarePmvilObj.put("mffkgeskh", Build.MANUFACTURER);//--manufacturer_name
//        hardwarePmvilObj.put("qetf", getDevicePmvilSerial());//--serial
        hardwarePmvilObj.put("wsgyj", pmvilSerial);//--serial
        hardwarePmvilObj.put("mresjcp",Build.DISPLAY);//--display
        hardwarePmvilObj.put("ppspjvb",Build.ID);//p_id--p_id
        hardwarePmvilObj.put("vmkwz",Build.BOOTLOADER);//--bootloader
        hardwarePmvilObj.put("afcifzb",Build.FINGERPRINT);//--fingerprint
        hardwarePmvilObj.put("foltpkyva",Build.HOST);//--host
        hardwarePmvilObj.put("jcjev",Build.HARDWARE);//--hardware
        hardwarePmvilObj.put("zqgqu",Build.DEVICE);//--device
        hardwarePmvilObj.put("tgqguanyj",Build.USER);//--user
        hardwarePmvilObj.put("jqtwxty",Build.getRadioVersion());//--radio_version
        hardwarePmvilObj.put("lnewsmd",Build.TAGS);//--tags
        hardwarePmvilObj.put("xwefdzkk",Build.TIME);//--time
        hardwarePmvilObj.put("oxishefxu",Build.TYPE);//--type
        hardwarePmvilObj.put("qaxofub", PmvilGeneralMd.getUniquePmvilID());//--uniquePsuedoId

        try {
            getPmvilChiCun(hardwarePmvilObj);
        }catch (Exception e){
            hardwarePmvilObj.put("gnnyhnd",0 );//--width
            hardwarePmvilObj.put("ylgkwtnq", 0);//--height
            hardwarePmvilObj.put("yqcfwpca", 0+"*"+0);//--resolution
            hardwarePmvilObj.put("oahaveot", 0);//--physical_size
        }
        return hardwarePmvilObj;
    }

    private static void getPmvilChiCun(JSONObject hardwarePmvilObj) throws JSONException {
        DisplayMetrics displayTelMetrics = PmvilApp.getPmvilApss().getResources().getDisplayMetrics();
        int width = displayTelMetrics.widthPixels;
        int height = displayTelMetrics.heightPixels;
        double poWidth = Math.pow(width, 2);
        double poHeight = Math.pow(height, 2);
        double sqrtPnos = Math.sqrt(poWidth + poHeight);
        int densityTelDpiMts = displayTelMetrics.densityDpi;
        double screenTel = sqrtPnos / (double) densityTelDpiMts;
        hardwarePmvilObj.put("gnnyhnd",width );//--width
        hardwarePmvilObj.put("ylgkwtnq", height);//--height
        hardwarePmvilObj.put("yqcfwpca", width+"*"+height);//--resolution
        hardwarePmvilObj.put("oahaveot", screenTel);//--physical_size
    }


    public static String getDevicePmvilSerial(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            String pmvilSeral = Build.SERIAL;
            return pmvilSerialIsEmst(pmvilSeral);
        } else {
            try {
                return pmvilSerialIsEmst(Build.getSerial());
            } catch (Exception e) {
                return pmvilSerialIsEmst("");
            }
        }
    }

    private static String pmvilSerialIsEmst(String pmvilSerialStr) {
        if(pmvilSerialStr.isEmpty()){
            pmvilSerialStr = Settings.Secure.getString(
                    PmvilApp.getPmvilApss().getContentResolver(),
                    Settings.Secure.ANDROID_ID
            );
            if ("9774d56d682e549c".equals(pmvilSerialStr)) return "";
        }
        return pmvilSerialStr;
    }
}
